#include "p2pms_client.hpp"
#include "p2pms_log.hpp"
#include "p2pms_util.hpp"

p2pms::IP2PMSClient* CreateP2PMSClient()
{
	return new p2pms::P2PMSClient();
}

void ReleaseP2PMSClient(p2pms::IP2PMSClient* client)
{
	delete client;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

namespace p2pms
{

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
P2PMSClient::P2PMSClient()
{
	// 设置日志级别
	rtc::LogMessage::LogToDebug(rtc::LoggingSeverity::LS_INFO);
	rtc::LogMessage::SetLogToStderr(true);

	m_sdk_thread = rtc::Thread::Create();
	m_sdk_thread->SetName("sdk_logic_thread", nullptr);
	if (!m_sdk_thread->Start()) {
		LOG_ERR("Failed to start SDK logic thread");
	} else {
		LOG_INF("SDK logic thread started successfully");
	}
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
P2PMSClient::~P2PMSClient()
{
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::Init(IMainThreadExecutor* executor, IClientHandler* handler, 
	const std::string& server_addr)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(),
		[&] { return m_impl.Init(executor, handler, server_addr, m_sdk_thread.get()); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::Login(const std::string& user_id)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), [&] { return m_impl.Login(user_id); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void P2PMSClient::Logout()
{
	return SyncProxy(m_sdk_thread.get(), [&] { m_impl.Logout(); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
std::vector<std::string> P2PMSClient::GetUserList()
{
	return SyncProxy<std::vector<std::string>>(m_sdk_thread.get(),
		[&] { return m_impl.GetUserList();  });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::MakeCall(CallMode mode, const std::string& callee_id)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), [&] { return m_impl.MakeCall(mode, callee_id); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void P2PMSClient::TerminateCall()
{
	return SyncProxy(m_sdk_thread.get(), [&] { m_impl.TerminateCall(); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::StartPublish(const MediaDevice& device, MediaInfo& media)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), 
		[&] { return m_impl.StartPublish(device, media); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::StopPublish(const MediaInfo& media)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), [&] { return m_impl.StopPublish(media); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::StartSubscribe(const std::string& user_id, const MediaInfo& media)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), 
		[&] { return m_impl.StartSubscribe(user_id, media); });
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
ErrCode P2PMSClient::StopSubscribe(const std::string& user_id, const MediaInfo& media)
{
	return SyncProxy<ErrCode>(m_sdk_thread.get(), 
		[&] { return m_impl.StopSubscribe(user_id, media); });
}

//--------------------------------------------------------------------------------------------------
// 获取渲染管理器
//--------------------------------------------------------------------------------------------------
IRendererManager* P2PMSClient::GetRendererManager()
{
	return m_impl.GetRendererManager();
}

//--------------------------------------------------------------------------------------------------
// 获取设备管理器
//--------------------------------------------------------------------------------------------------
IDeviceManager* P2PMSClient::GetDeviceManager()
{
	return m_impl.GetDeviceManager();
}

} // namespace p2pms